//
//  LHRecommendRestaurantDetailView.m
//  Knight_Tour
//
//  Created by 庞魁 on 15-5-16.
//  Copyright (c) 2015年 TouchOne. All rights reserved.
//

#import "LHRecommendRestaurantDetailView.h"
#import "UIImageView+WebCache.h"

@interface LHRecommendRestaurantDetailView ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIImageView *resPic;
@property (nonatomic, retain) UILabel *resName;
@property (nonatomic, retain) UILabel *resOpenTime;
@property (nonatomic, retain) UILabel *resAddress;
@property (nonatomic, retain) UILabel *splitLine1;
@property (nonatomic, retain) UILabel *introductionLabel;
@property (nonatomic, retain) UILabel *briefIntroduction;
@property (nonatomic, assign) CGFloat f;
@property (nonatomic, assign) CGFloat f1;
@property (nonatomic, retain) UIScrollView *resScrollView;

@end

@implementation LHRecommendRestaurantDetailView


- (void)dealloc
{
    [_resPic release];
    [_resName release];
    [_resOpenTime release];
    [_resAddress release];
    [_splitLine1 release];
    [_introductionLabel release];
    [_briefIntroduction release];
    [_resScrollView release];
    [super dealloc];
}


// 初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        // 创建scrollView
        self.resScrollView = [[UIScrollView alloc] init];
        
        _resScrollView.backgroundColor = [UIColor colorWithRed:0.871 green:0.962 blue:0.965 alpha:1.000];
        
        _resScrollView.contentSize = CGSizeMake(0, 800);
        
        _resScrollView.delegate = self;
        
        _resScrollView.showsHorizontalScrollIndicator = YES;
        
        [self addSubview:_resScrollView];
        
        [_resScrollView release];
        
        // 创建餐馆相框
        self.resPic = [[UIImageView alloc] init];
        
//        _resPic.backgroundColor = [UIColor colorWithRed:0.115 green:0.757 blue:0.815 alpha:1.000];
        
        [_resScrollView addSubview:_resPic];
        
        [_resPic release];
        
        // 餐馆名
        self.resName = [[UILabel alloc] init];
        
//        _resName.backgroundColor = [UIColor colorWithRed:0.220 green:0.665 blue:0.072 alpha:1.000];
        
        [_resScrollView addSubview:_resName];
        
        [_resName setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17]];
        
        [_resName release];
        
        // 收藏按钮
        self.collectionButton = [UIButton buttonWithType:UIButtonTypeSystem];
        
        _collectionButton.backgroundColor = [UIColor colorWithRed:0.871 green:0.962 blue:0.965 alpha:1.000];
        
        [_resScrollView addSubview:_collectionButton];
        
        // 餐馆地址
        self.resAddress = [[UILabel alloc] init];
        
//        _resAddress.backgroundColor = [UIColor colorWithRed:0.085 green:0.535 blue:0.862 alpha:1.000];
        
        [_resScrollView addSubview:_resAddress];
        
        [_resAddress setFont:[UIFont fontWithName:@"Helvetica-Bold" size:13]];
        
        [_resAddress release];
        
        // 开放时间
        self.resOpenTime = [[UILabel alloc] init];
        
//        _resOpenTime.backgroundColor = [UIColor colorWithRed:0.085 green:0.527 blue:0.839 alpha:1.000];
        
        [_resScrollView addSubview:_resOpenTime];
        
        [_resOpenTime setFont:[UIFont fontWithName:@"Helvetica-Bold" size:13]];
        
        [_resOpenTime release];
        
        // 分割线1
        self.splitLine1 = [[UILabel alloc] init];
        
        _splitLine1.backgroundColor = [UIColor colorWithRed:0.877 green:0.923 blue:0.967 alpha:1.000];
        
        [_resScrollView addSubview:_splitLine1];
        
        [_splitLine1 release];
        
        
        
        // 简介两个字label
        self.introductionLabel = [[UILabel alloc] init];
        
//        _introductionLabel.backgroundColor = [UIColor colorWithRed:0.164 green:0.329 blue:0.906 alpha:1.000];
        
        [_resScrollView addSubview:_introductionLabel];
        
        [_introductionLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
        
        [_introductionLabel release];
        
        // 简介
        self.briefIntroduction = [[UILabel alloc] init];
        
//        _briefIntroduction.backgroundColor = [UIColor colorWithRed:0.721 green:0.000 blue:0.969 alpha:1.000];
        
        [_resScrollView addSubview:_briefIntroduction];
        
        _briefIntroduction.numberOfLines = 0;
        
        [_briefIntroduction setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17]];
        
        [_briefIntroduction release];
        

        
    }
    
    
    return self;
}

/**
 *  设置控件布局信息
 */
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    
    
    _resPic.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height / 3);
    
    _resName.frame = CGRectMake(10, _resPic.frame.origin.y + self.frame.size.height / 3 + 20, 250, 30);
    
    _collectionButton.frame = CGRectMake(self.frame.size.width - 50, _resPic.frame.origin.y + self.frame.size.height / 3 + 20, 40, 40);
    _resAddress.frame = CGRectMake(10, _resName.frame.origin.y + 40, 250, 40);
    
    _resOpenTime.frame = CGRectMake(10, _resAddress.frame.origin.y + 30, 200, 20);
    
    _splitLine1.frame = CGRectMake(0, _resOpenTime.frame.origin.y + 30, self.frame.size.width, 3);
    
    _introductionLabel.frame = CGRectMake(10, _splitLine1.frame.origin.y + 13, 40, 30);
    
    _briefIntroduction.frame = CGRectMake(10, _introductionLabel.frame.origin.y + 50, self.frame.size.width - 20, _f);
    
    _resScrollView.frame = CGRectMake(0, 0, self.frame.size.width, self.bounds.size.height);
    
    CGFloat dy = _briefIntroduction.frame.size.height + self.briefIntroduction.frame.origin.y + 200;

    
    if (dy < self.bounds.size.height) {
        
        dy = self.bounds.size.height+20;
    }
    
    [_resScrollView setContentSize:CGSizeMake(0, dy)];
    
}



// 文字自适应高度
+ (CGFloat)heightWithText:(NSString *)text
{
    
    // 创建一个字典保存文本属性
    // 保存文本文字大小
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
    
    // 预设一个尺寸, 文本最大不会超过这个尺寸
    CGSize size = CGSizeMake(LHWidth - 20, 1000);
    
    // 根据文本信息获得一个CGRect
    // 参数1:尺寸范围  参数2:文本属性  参数3:按照什么方式获取Rect  参数4:nil
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return rect.size.height;
    
}

// 重写setter方法
- (void)setRecResModel:(LHRecommendRestaurantModel *)recResModel
{
    if (_recResModel != recResModel) {
    
        [_recResModel release];
        
        _recResModel = [recResModel retain];
        
    }
    
    [_resPic sd_setImageWithURL:[NSURL URLWithString:recResModel.cover] placeholderImage:[UIImage imageNamed:@"word4"]];
    
    _resName.text = recResModel.name;
    
    _resAddress.text = recResModel.address;
    _resAddress.numberOfLines = 2;
    [_resAddress setFont:[UIFont systemFontOfSize:13]];
    
    _resOpenTime.text = recResModel.opening_time;
    [_resOpenTime setFont:[UIFont systemFontOfSize:13]];
    
    _introductionLabel.text = @"简介";
    
    self.f = [[self class] heightWithText:recResModel.resDescription];
    _briefIntroduction.text = recResModel.resDescription;
    
}

// scrollview拖动时被调用

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat y = scrollView.contentOffset.y;
    
    
    if (y < 0) {
        
        _resPic.frame = CGRectMake(y / 2, y, self.frame.size.width - y, self.frame.size.height / 3 - y);
    }
    
    
}













/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
